#pragma once
#ifndef MYSPHERE_H
#define MYSPHERE_H
#include <vector>
#include <cmath>
#include "MyPrimitive.h"

class MySphere : public MyPrimitive {

public:

	~MySphere(void){};
	MySphere(MyMaterial mat,float x, float y, float z, float radius){
	
		
		this->m = mat;
		this->radius = radius;
		this->center.x = x;
		this->center.y = y;
		this->center.z = z;

	}

	MyVector3 getNormal(MyVector3 point);
	float x,y,z,radius;
	MyVector3 center;
	int whatIsIt();
	bool isIntersection(MyRay ray, float &t);
};

int MySphere::whatIsIt(){

	return 0;
}

MyVector3 MySphere::getNormal(MyVector3 point){

	MyVector3 u = point.minus(this->center);
	u.normalize();
	return u;
}

bool MySphere::isIntersection(MyRay ray, float &t){

	MyVector3 dst = ray.origin.minus(this->center);
	
	float b = dst.scalarMult(ray.direction);
	float c = dst.scalarMult(dst) - (this->radius * this->radius);
    float d = b * b -  c;
    
    if (d > 0.0f) {
		
        t = -b - sqrtf(d);
		if (t < 0.0f) {
			t = -b + sqrtf(d);
			if (t < 0.0f) {
				return false;
			}
		}
       
        return true;
        
    }
    return false;

	
}

#endif